export const dataToUrl = (str) => {
	function filter(str) { // 特殊字符转义
		str += ''; // 隐式转换
		str = str.replace(/%/g, '%25');
		str = str.replace(/\+/g, '%2B');
		str = str.replace(/ /g, '%20');
		str = str.replace(/\//g, '%2F');
		str = str.replace(/\?/g, '%3F');
		str = str.replace(/&/g, '%26');
		str = str.replace(/\=/g, '%3D');
		str = str.replace(/#/g, '%23');
		return str;
	}
	function formateObjToParamStr(paramObj) {
		const sdata = [];
		for (let attr in paramObj) {
			sdata.push(`${attr}=${filter(paramObj[attr])}`);
		}
		return sdata.join('&');
	};
	return formateObjToParamStr(str);
};
export const ajax = (option) => {
	const setting = {
		url: '',
		method: 'GET',
		headers: {},
		dataType: 'text',
		data: null,
		mimeType: 'text/plain; charset=gb2312',
		success(result) {},
		error(xhr, textStatus, error) {

		}
	};
	const config = Object.assign(setting, option);
	const xhr = new plus.net.XMLHttpRequest();

	xhr.setRequestHeader('Content-Type', config.dataType);
	for (let key in config.headers) {
		xhr.setRequestHeader(key, config.headers[key]);
	}

	// xhr.setRequestHeader(':authority', 'w12.qqv16.vip:5561')
	// xhr.setRequestHeader(':method', 'GET')
	// xhr.setRequestHeader(':path', '/luyilu/list_5_1.html')
	// xhr.setRequestHeader(':scheme', 'https')
	// xhr.setRequestHeader('accept',
	// 	'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8')
	// xhr.setRequestHeader('accept-encoding', ' gzip, deflate, br')
	// xhr.setRequestHeader('accept-language', 'zh-CN,zh;q=0.9')
	// xhr.setRequestHeader('cache-control', 'no-cache')
	// xhr.setRequestHeader('cookie',
	// 	'UM_distinctid=17bee5c95775ca-0d3032f2b9cb4b-33554574-384000-17bee5c9579909; CNZZDATA1254428444=195158945-1631787239-https%253A%252F%252Fdz.zhaifulifabu.com%253A9527%252F%7C1632908309; Hm_lvt_731a53a94394c1764ce2ab6cc1a76d2d=1631791650,1631793212,1631797530,1632913743; ASPSESSIONIDSCTSSDTB=IJJHPAGBMBFKNOEAEMMAJNEF; Hm_lpvt_731a53a94394c1764ce2ab6cc1a76d2d=1632914349'
	// 	)
	// xhr.setRequestHeader('pragma', 'no-cache')
	// xhr.setRequestHeader('upgrade-insecure-requests', 1)
	// xhr.setRequestHeader('user-agent',
	// 	'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3872.400 QQBrowser/10.8.4478.400'
	// 	)

	xhr.onreadystatechange = function() {
		switch (xhr.readyState) {
			case 0:
				// console.log("xhr请求已初始化");
				break;
			case 1:
				// console.log("xhr请求已打开");
				break;
			case 2:
				// console.log("xhr请求已发送");
				break;
			case 3:
				// console.log("xhr请求已响应");
				break;
			case 4:
				if (xhr.status == 200) {
					// console.log("xhr请求成功：" + xhr.responseText);
					config.success(xhr.responseText)

				} else {
					console.log("xhr请求失败：" + xhr.status);
					console.log(xhr.getAllResponseHeaders());
					config.error(xhr, xhr.status)
				}
				break;
		}
	}
	xhr.ontimeout = function() {
		// xhr请求超时事件处理
		config.error(xhr, 'timeout')
	};
	if (config.charset) {

	}
	xhr.overrideMimeType(config.mimeType);
	xhr.onerror = function(e) {
		console.log('status:' + xhr.status)
		const str = "lengthComputable=" + e.lengthComputable + "loaded=" + e.loaded + ";total=" + e.total;
		console.log("onerror: " + str);
		// console.log(JSON.stringify(e))
		// config.error(xhr, )
		// xhr请求出错事件处理
	};
	xhr.open(config.method, config.url);
	if (config.method === 'POST') {
		if (config.data) {
			// 发送HTTP请求
			xhr.send(JSON.stringify(config.data));
		} else {
			// 发送HTTP请求
			xhr.send();
		}
	} else {
		if (config.data) {
			if (config.url.indexOf('?')) {
				config.url = config.url + '&' + util.dataToUrl(config.data)
			} else {
				config.url = config.url + '?' + util.dataToUrl(config.data)
			}
		}
		// 发送HTTP请求
		xhr.send();
	}
}
